/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package job.manager.client.mb;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import job.manager.db.ec.UserGroupsEB;
import job.manager.db.ec.UsersEB;
import job.manager.db.sb.UserRemote;

/**
 *
 * @author Administrator
 */

public class UserMB {
    @EJB
    private UserRemote userBean;
    private UsersEB user;

    private String msg;

    /** Creates a new instance of UserMB */
    public UserMB() 
    {
        this.onInit();
    }

    @PostConstruct
    public void onInit()
    {
        this.user = new UsersEB();
        this.msg = "";
    }

    public void InsertUser(ActionEvent arg) {
        this.setValues();
        try {
            this.userBean.createUser(this.user);
            this.Clear(arg);
            this.msg = "Användare tillagd";            
        }catch(Exception e){
            this.msg = e.getMessage();
        }
    }

    public void setValues()
    {
        this.setUserName(this.user.getUserName());
        this.setPassword(user.getPassword());
        this.setEmail(user.getEmail());
    }

    public void validateEmail(FacesContext context, UIComponent validate, Object value){
        if( value instanceof String){
            String v = (String)value;

        }
        
    }

    public void Clear(ActionEvent arg){
        this.onInit();
    }

    /** Parameters **/
    public void setUserName(String username) {
        this.user.setUserName(username);
    }

    public String getUserName() {
        return this.user.getUserName();
    }

    public void setPassword(String password) {
        this.user.setPassword(password);
    }

    public String getPassword() {
        return this.user.getPassword();
    }

    public void setEmail(String email) {
        this.user.setEmail(email);
    }

    public String getEmail() {
        return this.user.getEmail();
    }

    public void setIsAdmin(boolean isAdmin) {
        this.user.setIsAdmin(isAdmin);
    }

    public boolean getIsAdmin() {
        return this.user.getIsAdmin();
    }

    public void setProfileId( Integer id ) {
        this.user.setProfileId(id);
    }

    public Integer getProfileId() {
        return this.user.getProfileId();
    }

    public String getUserGroupId() {
        if(this.user.getUsergroup() != null )
             return this.user.getUsergroup().getId().toString();
        return "";
    }

    public void setUserGroupId(String userGroupId) {
        int groupId = Integer.parseInt(userGroupId);
        UserGroupsEB ug = userBean.getUserGroup(groupId);
        this.user.setUsergroup(ug);
    }

    public String getMsg() {
        return msg;
    }
    
}
